package cn.iocoder.yudao.module.handguard.service.userfollowup;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.app.userfollowup.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.userfollowup.UserFollowUpDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.userfollowup.UserFollowUpConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.userfollowup.UserFollowUpMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 业务员/权证跟进记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserFollowUpServiceImpl implements UserFollowUpService {

    @Resource
    private UserFollowUpMapper userFollowUpMapper;

    @Override
    public Long createUserFollowUp(UserFollowUpCreateReqVO createReqVO) {
        // 插入
        UserFollowUpDO userFollowUp = UserFollowUpConvert.INSTANCE.convert(createReqVO);
        userFollowUpMapper.insert(userFollowUp);
        // 返回
        return userFollowUp.getId();
    }

    @Override
    public void updateUserFollowUp(UserFollowUpUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserFollowUpExists(updateReqVO.getId());
        // 更新
        UserFollowUpDO updateObj = UserFollowUpConvert.INSTANCE.convert(updateReqVO);
        userFollowUpMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserFollowUp(Long id) {
        // 校验存在
        validateUserFollowUpExists(id);
        // 删除
        userFollowUpMapper.deleteById(id);
    }

    private void validateUserFollowUpExists(Long id) {
        if (userFollowUpMapper.selectById(id) == null) {
            throw exception(USER_FOLLOW_UP_NOT_EXISTS);
        }
    }

    @Override
    public UserFollowUpDO getUserFollowUp(Long id) {
        return userFollowUpMapper.selectById(id);
    }

    @Override
    public List<UserFollowUpDO> getUserFollowUpList(Collection<Long> ids) {
        return userFollowUpMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserFollowUpDO> getUserFollowUpPage(UserFollowUpPageReqVO pageReqVO) {
        return userFollowUpMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserFollowUpDO> getUserFollowUpList(UserFollowUpExportReqVO exportReqVO) {
        return userFollowUpMapper.selectList(exportReqVO);
    }

    //userType 2：业务员 。3权证
    @Override
    public int getMyFollowUpUser(Long userId,Integer userType) {
        return userFollowUpMapper.getMyFollowUpUser(userId,userType);
    }

    @Override
    public int getMyMonthFollowUpUser(Long userId) {
        List<UserFollowUpDO> list = userFollowUpMapper.getMyMonthFollowUpUser(userId);
        return list.size();
    }

    @Override
    public List<UserFollowUpDO> getMonthFollowUpList(Long userId) {
        return userFollowUpMapper.getMonthFollowUpList(userId);
    }

    @Override
    public Integer getAWUpUser(Long userId) {
        List<UserFollowUpDO> list = userFollowUpMapper.getMyMonthFollowUpUser(userId);
        //userFollowUpMapper.getAWUpUser(userId);
        return list.size();
    }

}
